package com.produce.user.util;

import com.produce.user.dto.OrganizationalStructureDto;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：实现组装组织架构树的数据
 * @author linzf
 */
public class OrgInstall {

    /**
     * 功能描述：实现组织架构的数据的组装
     * @return
     */
    public static List<OrganizationalStructureDto> installOrg(List<OrganizationalStructureDto> organizationalStructureDtoList){
        List<OrganizationalStructureDto> organizationalStructureDtos = new ArrayList<>();
        for(OrganizationalStructureDto organizationalStructureDto:organizationalStructureDtoList){
            if("0".equals(organizationalStructureDto.getOrgParentId())){
                organizationalStructureDto.setChildren(getChild(organizationalStructureDto.getOrgId(),organizationalStructureDtoList));
                organizationalStructureDtos.add(organizationalStructureDto);
            }
        }
        return organizationalStructureDtos;
    }

    /**
     * 功能描述：递归遍历组织架构节点
     * @param orgId
     * @param organizationalStructures
     * @return
     */
    private static List<OrganizationalStructureDto> getChild(String orgId, List<OrganizationalStructureDto> organizationalStructures) {
        List<OrganizationalStructureDto> childList = new ArrayList<>();
        for (OrganizationalStructureDto organizationalStructureDto : organizationalStructures) {
            if(organizationalStructureDto.getOrgParentId().equals(orgId)){
                organizationalStructureDto.setChildren(getChild(organizationalStructureDto.getOrgId(),organizationalStructures));
                childList.add(organizationalStructureDto);
            }
        }
        return childList;
    }

}
